<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
  <meta charset="utf-8">
  <title>角色管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/adminui/dist/css/admin.css}" media="all">
  <link rel="stylesheet" th:href="@{/style/custom.css}" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto layui-form-pane" lay-filter="LAY-role-front-form">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">角色编码</label>
          <div class="layui-input-block">
            <input type="text" name="roleId" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">角色名称</label>
          <div class="layui-input-block">
            <input type="text" name="roleName" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">状态</label>
          <div class="layui-input-block">
            <select name="status" id="status" lay-search>
              <option value="">全部</option>
              <option th:each="map,idx:${statusList}" th:value="${map.key}" th:text="${map.value}"></option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="LAY-role-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
          </button>
          <button class="layui-btn layui-btn-primary layui-btn-sm" lay-submit lay-filter="LAY-role-front-reset">
            <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i> 重置
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div class="layui-btn-group" style="padding-bottom: 10px;">
        <!--刷新table-->
        <button type="button" class="layui-btn layui-btn-normal btn-op-role layui-btn-sm" data-type="refresh">
          <i class="layui-icon">&#xe666;</i>刷新
        </button>
        <!-- 新增 -->
        <button sec:authorize="hasAuthority('sys:role:add')" type="button" class="layui-btn layui-btn-normal btn-op-role layui-btn-sm" data-type="add">
          <i class="layui-icon">&#xe654;</i>新增
        </button>
      </div>

      <!--table 定义-->
      <table id="LAY-role-table" lay-filter="LAY-role-table"></table>
      <!--table 列转换-->
      <script type="text/html" id="statusSwitchTpl">
        <input type="checkbox" disabled name="status" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" {{ d.status== 'Y' ?'checked' : '' }}>
      </script>

      <!--列表操作项-->
      <script type="text/html" id="role-table">
        <a sec:authorize="hasAuthority('sys:role:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="more">更多 <i class="layui-icon layui-icon-down"></i></a>
      </script>
    </div>
  </div>
</div>

<script th:src="@{/layui/layui.js}"></script>
<script>
  layui.config({
    base: '/'
  }).extend({
    //设定模块别名，模块名是使用layui.模块名  模块文件是 js名字。不设置模块名 = 模块文件名
    role: 'modules/sys/role'
  }).use(['role'], function(){
    let form = layui.form;
    let admin = layui.admin;
    let table = layui.table;
    let layer = layui.layer;
    let dropdown = layui.dropdown;
    let role = layui.role;

    //监听搜索
    form.on('submit(LAY-role-front-search)', function (data) {
      let param = data.field;
      //执行重载
      table.reload('LAY-role-table', {
        where: {param}
      });
    });
    // 监听重置
    form.on('submit(LAY-role-front-reset)', function (data) {
      let field = data.field;
      for (let key of Object.keys(field)) {
        field[key] = '';
      }
      form.val("LAY-role-front-form", field);
    });

    //事件
    let active = {
      add: function () {
        layer.open({
          type: 2
          , title: '新增'
          , content: '/sys/role/turn/edit'
          // ,maxmin: true
          , resize: false
          , area: ['500px', '540px']
          , btn: ['确定', '取消']
          , yes: function (index, layero) {
            role.handlesSave(index,layero);
          }
        });
      }
      , refresh: function () {
        let param = form.val("LAY-role-front-form");
        //执行重载 数据刷新
        table.reload('LAY-role-table', {
          where: {param}
        });
      }
    };

    //执行事件操作
    layui.$('.layui-btn.btn-op-role').on('click', function () {
      let type = layui.$(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(LAY-role-table)', function (obj) {
      let line_data = obj.data;

      if (obj.event === 'edit') {
        // let tr = $(obj.tr);
        layer.open({
          type: 2
          , title: '修改'
          , content: '/sys/role/turn/edit?id=' + line_data.id
          // ,maxmin: true
          , resize: false
          , area: ['500px', '540px']
          , btn: ['确定', '取消']
          , yes: function (index, layero) {
            role.handlesSave(index,layero);
          }
          , success: function (layero, index) {

          }
        });
      } else if (obj.event === 'more') {
        let more = new Array();
        if([[${#authorization.expression('hasAuthority("sys:role:permit")')}]]) {
          more.push({
            title: '权限',
            id: 'perms',
            templet: '<i class="layui-icon layui-icon-list"></i> 权限',
            href: '#'
          })
        }
        if([[${#authorization.expression('hasAuthority("sys:role:del")')}]]) {
          more.push({
            title: "删除",
            id: "del",
            templet: '<i class="layui-icon layui-icon-delete"></i> 删除',
            href: '#'
          })
        }

        //下拉菜单
        dropdown.render({
          elem: this, // 触发事件的 DOM 对象
          show: true, // 外部事件触发即显示
          align: 'left ' , // 右对齐弹出
          data: more,
          click: function (mdata) {
            if (mdata.id === "del") {
              if ('10001' == line_data.roleId) {
                layer.msg('超级管理员角色不允许删除！', {icon: 4, time: 1000, shade: [0.3, '#000', true]});
                return;
              }

              layer.confirm('真的删除行么？', function (index) {
                role.handleDelete(index, line_data.id);
              });
            } else if (mdata.id === "perms") {
              layer.open({
                type: 2
                , title: '权限分配'
                , content: '/sys/role/turn/perms?roleId=' + line_data.roleId
                , area: ['550px', '100%']
                , resize: false
                , btn: ['确认', '取消']
                , btnAlign: 'r'
                , maxmin: true
                , offset: 'r'
                , anim: 'slideLeft' // 从右往左
                , yes: function (index, layero) {
                  role.handlePerms(index, layero);
                }
                , btn2: function (index, layero) {
                  layer.close(index);
                }
                , end: function () {
                  //数据刷新
                  table.reload('LAY-role-table');
                }
              });
            }
          }
        });
      }
    });
  });
</script>
</body>
</html>
